<?php
//Uploading Images
$db = new Database();
$path = "images/logo/";
$auth = Auth::getAuth('current_user');
$userID = $auth['id'];
$valid_formats = array("jpg", "png", "gif", "bmp");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
    $name = $_FILES['photoimg']['name'];
    $size = $_FILES['photoimg']['size'];
    
    if(strlen($name))
    {
            list($txt, $ext) = explode(".", $name);
            if(in_array($ext,$valid_formats))
            {
            if($size<(1024*1024))
                    {
                            $actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
                            $tmp = $_FILES['photoimg']['tmp_name'];
                            if(move_uploaded_file($tmp, $path.$actual_image_name))
                                    {
					$location = $path.$actual_image_name;
					$getSettings = $db->query("SELECT * FROM tbsettings","array");
					foreach($getSettings as $value){
					    $imageLogoPath = $value['logo'];
					}
					unlink($imageLogoPath); //Erase file from the directory
					//Update Record from the database
					$con = 1;
					$fields = array("logo"=>$location);
					$conditions = array("id"=>$con);
					$db->update("tbsettings",$fields,$conditions);
					$timezone = "Asia/Manila";
					if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
					$audit = array(
								"user_id"=>$userID,
								"action_id"=>15,
								"affected_table"=>"tbsettings",
								"affected_record"=>"logo",
								"datetime"=>date("Y-m-d  H:i:s"),
								"is_active"=>1
								);
					$db->insert("tbaudit_trail",$audit);
                                        //echo "<img src='images/logo/".$actual_image_name."'  class='preview'>";
					echo '
					<div class="notification_wrapper1">
					    <div class="correct">
						<img src="images/warning/correct.png" width="22" height="19" class="img-notification-position">
						<div class="notification-position" id="correct">    
						    Record was successfully updated.
						</div>
					   </div>
					</div>';
					?>
					 <meta http-equiv="refresh" content="1;URL='/settings'" />    
					<?php
                                    }
                            else
                                    echo "failed";
                    }
                    else
                    echo "Image file size max 1 MB";					
                    }
                    else
                    echo "Invalid file format..";	
    }
    else
    exit;
}